package com.alibaba.algo.components.permission.controller;

import com.alibaba.algo.components.permission.dto.SysMenuDTO;
import com.alibaba.algo.components.permission.dto.SysUserDTO;
import com.alibaba.algo.components.permission.service.SysMenuService;
import com.alibaba.algo.components.permission.service.SysUserService;
import com.alibaba.algo.components.permission.service.TokenService;
import com.alibaba.algo.components.permission.vo.LoginVO;
import com.alibaba.algo.components.permission.vo.SysMenuVO;
import com.alibaba.algo.core.contant.SysConstant;
import com.alibaba.algo.core.resp.BaseResult;
import com.alibaba.algo.util.PasswordUtils;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;

/**
 * Created by IntelliJ IDEA.
 * User: glorysongglory
 * Date: 2020/11/5
 * Time: 17:04
 */
@RestController
@RequestMapping(value = "/permission")
public class PermissionController {

    @Autowired
    SysUserService sysUserService;

    @Autowired
    TokenService tokenService;

    @Autowired
    SysMenuService sysMenuService;


    @PostMapping("/login")
    public BaseResult login(@RequestBody LoginVO loginVO) {
        JSONObject jsonObject = new JSONObject();
        String encodePasswd = PasswordUtils.encode(loginVO.getPasswd(), SysConstant.SYS_PERMISSION_PASSWD_SALT);
        SysUserDTO sysUserDTO = new SysUserDTO();
        sysUserDTO.setName(loginVO.getName());
        sysUserDTO.setPasswd(encodePasswd);
        List<SysUserDTO> sysUserList = sysUserService.listByPara(sysUserDTO);
        if (sysUserList.size() == 0) {
            return BaseResult.buildError("登录失败,用户不存在");
        } else {
            SysUserDTO sysUserDTORet = sysUserList.get(0);
            String subject = SysConstant.SYS_PERMISSION_JWT_TOKEN_SUBJECT;
            String audience = String.valueOf(System.currentTimeMillis());
            String sign = PasswordUtils.getUuid();

            String token = tokenService.generateToken(subject, audience, sign, 7200);
            // 初始化token对应的业务数据
            tokenService.initTokenBizData(token, sysUserDTORet);
            jsonObject.put("token", token);
            jsonObject.put("user", new HashMap() {{
                put("name", sysUserDTORet.getName());
            }});
            return BaseResult.buildSuccess(jsonObject);
        }
    }

    @GetMapping("/getMenu")
    public BaseResult getMenus(HttpServletRequest httpServletRequest) {
        Long userId = tokenService.getUserIdFromHttpServletRequest(httpServletRequest);
        SysMenuDTO sysMenuDTO = sysMenuService.getMenu(userId);
        return BaseResult.buildSuccess(menuDTO2menuVO(sysMenuDTO).getRoutes());
    }

    private SysMenuVO menuDTO2menuVO(SysMenuDTO sysMenuDTO) {
        SysMenuVO sysMenuVO = new SysMenuVO();
        sysMenuVO.setIcon(sysMenuDTO.getIcon());
        sysMenuVO.setName(sysMenuDTO.getName());
        sysMenuVO.setPath(sysMenuDTO.getRoute());
        List<SysMenuDTO> sysMenuDTOChildList = sysMenuDTO.getChildList();
        if (null != sysMenuDTOChildList && sysMenuDTOChildList.size() > 0) {
            List<SysMenuVO> sysMenuVOChildList=sysMenuDTOChildList.stream().map(dto->menuDTO2menuVO(dto)).collect(Collectors.toList());
            sysMenuVO.setRoutes(sysMenuVOChildList);
        }
        return sysMenuVO;
    }

    @GetMapping("/logout")
    public BaseResult logout(HttpServletRequest httpServletRequest) {
        String token = tokenService.getTokenFromHttpServletRequest(httpServletRequest);
        tokenService.invalidToken(token);
        return BaseResult.buildSuccess("退出成功");
    }

}
